package view.Core;

import fyp.mobile.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

public class TabActivityX extends TabActivity {
	/** Called when the activity is first created. */
	TabWidget mTabWidget;
	String treeID = "T0001";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scroll_view);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;
		
        Bundle bunde = this.getIntent().getExtras();
        treeID = bunde.getString("treeID");
		

		// A
		intent = new Intent().setClass(this, TabActivityA.class);
		intent.putExtra("treeID", treeID);
		spec = tabHost
				.newTabSpec("Info")
				.setIndicator("Info",
						res.getDrawable(R.drawable.treeicon_main))
				.setContent(intent);

		// tabHost.setSoundEffectsEnabled(true);
		tabHost.addTab(spec);

		// B
		intent = new Intent().setClass(this, TabActivityB.class);
		intent.putExtra("treeImgID", treeID);
		spec = tabHost
				.newTabSpec("Image")
				.setIndicator("Image",
						res.getDrawable(R.drawable.treeicon_main))
				.setContent(intent);
		tabHost.addTab(spec);

		// C
		intent = new Intent().setClass(this, TabActivityC.class);
		intent.putExtra("treeIDForMap", treeID);
		spec = tabHost
				.newTabSpec("Map")
				.setIndicator("Map",
						res.getDrawable(R.drawable.treeicon_map))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setBackgroundColor(0);

		// tabHost.setBackgroundDrawable(getWallpaper());
		tabHost.setCurrentTab(0);

	}

	private void show_alert() {

		AlertDialog.Builder alert_box = new AlertDialog.Builder(this);
		alert_box.setMessage("Do you want to quit the app?");
		alert_box.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						onDestroy();
					}
				});

		alert_box.setNegativeButton("No",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		alert_box.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "Comment").setShortcut('0',
				'a');
		menu.add(Menu.NONE, Menu.FIRST + 2, Menu.NONE, "History").setShortcut('1',
				'b');
		menu.add(Menu.NONE, Menu.FIRST + 3, Menu.NONE, "Quit").setShortcut('2',
				'c');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			Intent intent = new Intent();
			intent.setClass(this, CommentActivity.class);
			intent.putExtra("treeID", treeID);
			startActivity(intent);
			break;
		case Menu.FIRST + 2:
			Intent intentHistory = new Intent();
			intentHistory.setClass(this, HistoryActivity.class);
			intentHistory.putExtra("treeID", treeID);
			startActivity(intentHistory);
			break;
		case Menu.FIRST + 3:
			show_alert();
			break;
		}

		return super.onOptionsItemSelected(item);

	}

	public void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}